package com.jiaming.restaurant.api.restaurant.controller;

import cn.hutool.core.util.StrUtil;
import com.jiaming.restaurant.api.common.bean.contants.CommonContants;
import com.jiaming.restaurant.api.common.bean.vo.MyPageVO;
import com.jiaming.restaurant.api.common.controller.BaseController;
import com.jiaming.restaurant.api.common.response.ResultCode;
import com.jiaming.restaurant.api.goods.bean.entity.Goods;
import com.jiaming.restaurant.api.goods.bean.vo.GoodsVO;
import com.jiaming.restaurant.api.restaurant.bean.entity.Restaurant;
import com.jiaming.restaurant.api.restaurant.bean.vo.PageVO;
import com.jiaming.restaurant.api.restaurant.bean.vo.RestaurantVO;
import com.jiaming.restaurant.api.restaurant.service.IRestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dragon
 * @create 2020-12-28 14:59
 */
@RestController
@RequestMapping(value = "/rest")
public class RestaurantController extends BaseController {

    @Autowired
    IRestaurantService restaurantService;

    @PostMapping(value = "/save")
    public Object save(@RequestBody Restaurant restaurant) {
        Map<String, Object> result = new HashMap<>();
        // 参数安全校验
//        String name = restaurant.getName();
//        String address = restaurant.getAddress();
//        if (StrUtil.isEmpty(name) || name.length() < 6 || name.length() > 40
//                || StrUtil.isEmpty(address)  || address.length() < 10 || address.length() > 80) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
//        Integer disable = restaurant.getDisable();
//        if (disable == null || disable < 0 || disable > 1) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        boolean isOk = restaurantService.save(restaurant);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        result.put("data", restaurant.getId());
        return result;
    }

    @PostMapping(value = "/updateById")
    public Object update(@RequestBody Restaurant restaurant) {
        Map<String, Object> result = new HashMap<>();
        // 参数安全校验
//        String name = restaurant.getName();
//        String address = restaurant.getAddress();
//        if (StrUtil.isNotEmpty(name) && (name.length() < 6 || name.length() > 40)) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
//        if (StrUtil.isNotEmpty(address) && (address.length() < 10 || address.length() > 80)) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
//        Integer disable = restaurant.getDisable();
//        if (disable != null && (disable < 0 || disable > 1)) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
//        Integer id = restaurant.getId();
//        if (id == null || id <= 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        boolean isOk = restaurantService.updateById(restaurant);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        return result;
    }

    @PostMapping(value = "/getById")
    public Object get(@RequestParam Integer id) {
        Map<String, Object> result = new HashMap<>();
//        if (id == null || id <= 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        Restaurant data = restaurantService.getById(id);
        result.put("code", ResultCode.SUCCESS);
        result.put("data", data);
        return result;
    }

    @PostMapping(value = "/deleteById")
    public Object delete(@RequestParam Integer id) {
        Map<String, Object> result = new HashMap<>();
//        Integer id = restaurant.getId();
//        if (id == null || id <= 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        boolean byId = restaurantService.removeById(id);
        result.put("code", ResultCode.SUCCESS);
        result.put("data", byId);
        return result;
    }

    @PostMapping(value = "/page")
    public Object page(@RequestBody PageVO pageVO) {
//        Integer pageNum = goods.getPageNum();
//        if (pageNum == null || pageNum <= 0) {
//            goods.setPageNum(1);
//        }
//        Long pageSize = goods.getPageSize();
//        if (pageSize == null || pageSize <= CommonContants.MIN_PAGE_SIZE) {
//            goods.setPageSize(CommonContants.MIN_PAGE_SIZE);
//        }
        MyPageVO<Restaurant> data = restaurantService.page(pageVO);
        Map<String, Object> result = new HashMap<>();
        result.put("code", ResultCode.SUCCESS);
        result.put("data", data);
        return result;
    }

}
